package com.nwpu.coursesupport.tool;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nwpu.coursesupport.controller.UserController;

public class JSONUtils {
		
    	protected static Logger logger = LoggerFactory.getLogger(JSONUtils.class);
		
	 	private final static ObjectMapper objectMapper = new ObjectMapper();  
	
	    /*public static String bean2Json(Object obj) throws IOException {  
	        StringWriter sw = new StringWriter();  
	        JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);  
	        mapper.writeValue(gen, obj);  
	        gen.close();  
	        return sw.toString();  
	    }  
	  
	    public static <T> T json2Bean(String jsonStr, Class<T> objClass)  
	            throws JsonParseException, JsonMappingException, IOException {  
	        return mapper.readValue(jsonStr, objClass);  
	    }  */
	  
	    private JSONUtils() {  
	  
	    }  
	  
	    public static ObjectMapper getInstance() {  
	  
	        return objectMapper;  
	    }  
	  
	    /** 
	     * javaBean,list,array convert to json string 
	     * @throws JsonProcessingException 
	     */  
	    public static String obj2json(Object obj)  {  
	        try {
				return objectMapper.writeValueAsString(obj);
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	        return null;
	    }  
	  
	    /** 
	     * json string convert to javaBean 
	     */  
	    public static <T> T json2pojo(String jsonStr, Class<T> clazz){  
	        try {
				return objectMapper.readValue(jsonStr, clazz);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error("json转换失败");
				//e.printStackTrace();
			}  
	        return null;
	    }  
	  
	    /** 
	     * json string convert to map 
	     */  
	    public static <T> Map<String, Object> json2map(String jsonStr)  
	            throws Exception {  
	        return objectMapper.readValue(jsonStr, Map.class);  
	    }  
	  
	    /** 
	     * json string convert to map with javaBean 
	     */  
	    public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz)  
	            throws Exception {  
	        Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr,  
	                new TypeReference<Map<String, T>>() {  
	                });  
	        Map<String, T> result = new HashMap<String, T>();  
	        for (Entry<String, Map<String, Object>> entry : map.entrySet()) {  
	            result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));  
	        }  
	        return result;  
	    }  
	  
	    /** 
	     * json array string convert to list with javaBean 
	     */  
	    public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz)  
	            throws Exception {  
	        List<Map<String, Object>> list = objectMapper.readValue(jsonArrayStr,  
	                new TypeReference<List<T>>() {  
	                });  
	        List<T> result = new ArrayList<T>();  
	        for (Map<String, Object> map : list) {  
	            result.add(map2pojo(map, clazz));  
	        }  
	        return result;  
	    }  
	  
	    /** 
	     * map convert to javaBean 
	     */  
	    public static <T> T map2pojo(Map map, Class<T> clazz) {  
	        return objectMapper.convertValue(map, clazz);  
	    }  

}
